$(function() {

	//登录
	$('#submit').click(function(event) {
						var username = $("#username").val();
						var password = $("#password").val();
						$.ajax({
									url : getRootPath()+'/checkLogin',
									data : {
										username : username,
										password : password
									},
									method : 'post',
									async : false,
									success : function(resultdata) {
										if (resultdata.status == 200) {
											//登录成功
											window.location.href = getRootPath()+"/admin/index";
										} else {
											//登录错误
											alert(resultdata.message);
										}
									},
									error : function() {
										alert("系统出错");
									}
								});
					});
	//注册
	$("#regsubmit").click(function(event) {
		
		var issubmit = check();
		if(issubmit){

			var username = $("#regusername").val();
			var password = $("#regpassword").val();
			$.ajax({
						url : getRootPath()+'/addBloger',
						data : {
							username : username,
							password : password
						},
						method : 'post',
						async : false,
						dataType:'json',
						success : function(resultdata) {
							if (resultdata.status == 200) {
								//注册成功
								alert(resultdata.message);
								window.location.href = getRootPath()+"/"
							} else {
								//注册失败
								alert(resultdata.message);
							}
						},
						error : function() {
							alert("系统出错");
						}
					});
		}
		
	})
	//验证用户名的正则表达式和验证用户名是否重复
	$("#regusername").blur(function (event){
		//获得出发这个事件的对象
		var obj = $(event.target);
		
		var isnext = regular(obj);
		if(isnext){
			$.ajax({
				url:getRootPath()+'/isRepetitionByName',
				data : {
					username : obj.val()
				},
				method:'post',
				async:false,
				dataType:'json',
				success:function (result){
					var tipobj =obj.next();
					if(result.status == 500 ){
						tipobj.html("该账号已经被注册");
						obj.attr("state","0");
					}else{
						tipobj.html("");
						obj.attr("state","1");
					}
				},
				error:function (){
					alert("系统出错");
				}
			});
		}
	});
	//验证密码的正则表达式
	$('#reppassword').blur(function (event){
		var obj = $(event.target);
		//获得注册重复密码
		var thisval = obj.val();
		//获得注册密码
		var passwordval = $('#regpassword').val();
		
		var isequal = thisval == passwordval;
		
		if(isequal){
			obj.next().html("");
			obj.attr("state","1");
		}else{
			obj.attr("state","0");
			if(thisval==""){
				obj.next().html(obj.attr("tip1"));
			}else{
				obj.next().html(obj.attr('tip'));
			}
		}
		
		
	})
	
	$("#regpassword").blur(function (event){
		var obj = $(event.target);
		var isnext = regular(obj);
	});
	
	
	
})


/*获得当前项目名*/
function getRootPath(){
		//获取当前网址，
		var curWwwPath=window.document.location.href;
		//获取主机地址之后的目录
		var pathName=window.document.location.pathname;
		var pos=curWwwPath.indexOf(pathName);
		//获取主机地址，
		var localhostPaht=curWwwPath.substring(0,pos);
		//获取带"/"的项目名，
		var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);
		return(projectName);
	}
//正则表达式验证
function regular(obj){
	var obj = $(obj);
	
	var reg = new RegExp(obj.attr("reg"));
	var tip = obj.attr("tip");
	var val = obj.val();
	var tipobj = obj.next();
	
	//判断正则表达式
	var is = reg.test(val);
	if(!is){
		tipobj.html(tip);
		obj.attr("state","0");
		return false;
	}else{
		tipobj.html("");
		obj.attr("state","1");
		return true;
	}
}
function check(){

	//判断状态码
	var reppasswordstate = $("#reppassword").attr("state");
	var passwordstate = $("#regpassword").attr("state");
	var usernamestate = $("#regusername").attr("state");
	if(!(reppasswordstate==1 && passwordstate==1 &&usernamestate==1 )){
		return false;
	}else{
		return true;
	}
}